﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Wicresoft.Common;
using Wicresoft.JDPostBoard.Entity;
public partial class Gift_GiftEdit : BaseAuthenticationPage
{
    protected string GiftID
    {
        get
        {
            if (ViewState["GiftID"] == null)
            {
                if (Request["GiftID"] == null || Request["GiftID"] == "")
                    ViewState["GiftID"] = Guid.NewGuid().ToString();
                else
                    ViewState["GiftID"] = Request["GiftID"];
            }

            return ViewState["GiftID"].ToString();
        }
    }

    protected string OpenType
    {
        get
        {
            return Request["Type"];
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (OpenType == "Add")
                this.Title = "新增礼品";
            else
                this.Title = "编辑礼品";

            InitControl();
            LoadDataFromEntity();
        }
    }

    protected void InitControl()
    {
        this.ddlGiftUnit.DataSource = DataAccess.Select(typeof(GiftUnit), " IsEnable='true' ", "UnitName");
        this.ddlGiftUnit.DataTextField = "UnitName";
        this.ddlGiftUnit.DataValueField = "UnitName";
        this.ddlGiftUnit.DataBind();
    }

    protected void LoadDataFromEntity()
    {
        Gift gift = new Gift(new Guid(GiftID));
        gift.Load();
        this.tbGiftName.Text = gift.GiftName;
        this.tbGiftPrice.Text = gift.GiftPrice.ToString();
        this.ddlGiftUnit.SelectedValue = gift.GiftUnit;
    }

    protected void lbtnSave_Click(object sender, EventArgs e)
    {
        SaveDataByEntity();
    }

    protected void SaveDataByEntity()
    {
        Gift gift = new Gift();
        gift.ID = new Guid(GiftID);
        if (!gift.Load("ID='" + GiftID + "'"))
        {
            gift.CreateTime = DateTime.Now;
            gift.CreatorID = CurrentUser.ID.ToString();
            gift.CreatorName = CurrentUser.Name;
            gift.CreatorDsName = CurrentUser.DisplayName;
            gift.IsEnable = true;
        }
        gift.GiftName = this.tbGiftName.Text;
        gift.GiftPrice = Convert.ToDouble(this.tbGiftPrice.Text);
        gift.GiftUnit = this.ddlGiftUnit.SelectedItem.Text;
        gift.Save();

        if (OpenType == "Edit2")
        {
            string script = "<script>alert('保存成功!');parent.document.getElementById('ctl00$ListToolBarPlaceHolder$btnRefresh').click();</script>";
            Page.RegisterStartupScript("refresh", script);
        }
        else
        {
            string script = "<script>alert('保存成功!');refreshParent();window.close();</script>";
            Page.RegisterStartupScript("refresh", script);
        }
    }
}
